#delimit;
clear all;
set more off;


use "Elec_Conf_Nationscape.dta", clear;


tostring start_date, gen(string_date_h) usedisplayformat force;
gen day = substr(string_date_h, 1, 2);
gen month = substr(string_date_h, 3, 3);
gen year = substr(string_date_h, 6, 4);
drop string_date_h;

destring day, replace;

gen pre_elec = .;
replace pre_elec = 1 if month == "oct";
replace pre_elec = 1 if month == "nov" & day <= 3;
replace pre_elec = 0 if month == "nov" & day >= 4;

gen post_elec = 0;
replace post_elec = 0 if month == "oct";
replace post_elec = 0 if month == "nov" & day <= 3;
replace post_elec = 1 if month == "nov" & day >= 4;
replace post_elec = 1 if month == "dec" | month == "jan";


gen trump_voter = .;
replace trump_voter = 1 if vote_2020 == 1 | vote_2020_retro == 1;
replace trump_voter = 0 if vote_2020 == 2 | vote_2020 == 3 | vote_2020 == 4 | vote_2020 == 999;
replace trump_voter = 0 if vote_2020_retro == 2 | vote_2020_retro == 3 | vote_2020_retro == 4 | vote_2020_retro == 5 | vote_2020_retro == 999;

gen biden_voter = .;
replace biden_voter = 1 if vote_2020 == 2 | vote_2020_retro == 2;
replace biden_voter = 0 if vote_2020 == 1 | vote_2020 == 3 | vote_2020 == 4 | vote_2020 == 999;
replace biden_voter = 0 if vote_2020_retro == 1 | vote_2020_retro == 3 | vote_2020_retro == 4 | vote_2020_retro == 5 | vote_2020_retro == 999;



gen elec_conf_4p = .;
replace elec_conf_4p = 4 if elect_conf_conduct_retro   == 1 | elect_conf_conduct   == 1;
replace elec_conf_4p = 3 if elect_conf_conduct_retro   == 2 | elect_conf_conduct   == 2;
replace elec_conf_4p = 2 if elect_conf_conduct_retro   == 3 | elect_conf_conduct   == 3;
replace elec_conf_4p = 1 if elect_conf_conduct_retro   == 4 | elect_conf_conduct   == 4;

gen elec_vconf = .;
replace elec_vconf = 1 if elect_conf_conduct_retr == 1 |  elect_conf_conduct   == 1;
replace elec_vconf = 0 if elect_conf_conduct_retr == 2 |  elect_conf_conduct   ==  2;
replace elec_vconf = 0 if elect_conf_conduct_retro   == 3 | elect_conf_conduct   == 3;
replace elec_vconf = 0 if elect_conf_conduct_retro   == 4 | elect_conf_conduct   == 4;



gen trumpv_post = trump_voter*post_elec;
gen bidenv_post = biden_voter*post_elec;

sum elec_vconf [aweight = weight] if biden_voter == 1 & post_elec == 1;
sum elec_vconf [aweight = weight] if biden_voter == 1 & pre_elec == 1;

sum elec_vconf [aweight = weight] if trump_voter == 1 & post_elec == 1;
sum elec_vconf [aweight = weight] if trump_voter == 1 & pre_elec == 1;

sum elec_vconf [aweight = weight] if biden_voter == 1 & post_elec == 1 & state == "GA";
sum elec_vconf [aweight = weight] if biden_voter == 1 & pre_elec == 1 & state == "GA";

sum elec_vconf [aweight = weight] if trump_voter == 1 & post_elec == 1 & state == "GA";
sum elec_vconf [aweight = weight] if trump_voter == 1 & pre_elec == 1 & state == "GA";



*Very Confident in Election Integrity Outcome;
reg elec_vconf trumpv_post trump_voter bidenv_post biden_voter post_elec [aweight = weight], robust;
predict elec_vconf_hat, xb;
predict se_elec_vconf_hat, stdp ;
gen lb_ci95_elec_vconf_hat = elec_vconf_hat - invttail(`e(N)', 0.975)*se_elec_vconf_hat;
gen ub_ci95_elec_vconf_hat = elec_vconf_hat + invttail( `e(N)', 0.975)*se_elec_vconf_hat;

*D-i-D estimator;
lincom bidenv_post - biden_voter - trumpv_post - trump_voter;

gen post_elec_ax = post_elec if trump_voter == 1;
replace post_elec_ax = post_elec-0.1 if biden_voter == 1;
replace post_elec_ax = post_elec+0.1 if biden_voter == 0 & trump_voter == 0;

#delimit;
twoway (scatter elec_vconf_hat post_elec_ax if post_elec == 1 & trump_voter == 1, msymbol(X) mcolor(red)) 
(scatter elec_vconf_hat post_elec_ax if post_elec == 0 & trump_voter == 1, msymbol(X) mcolor(red)) 
(rcap lb_ci95_elec_vconf_hat ub_ci95_elec_vconf_hat post_elec_ax if post_elec == 1 & trump_voter == 1, lcolor(gs6))
(rcap lb_ci95_elec_vconf_hat ub_ci95_elec_vconf_hat post_elec_ax if post_elec == 0 & trump_voter == 1, lcolor(gs6))
(scatter elec_vconf_hat post_elec_ax if post_elec == 1 & biden_voter == 1, msymbol(D) mcolor(blue))  
(scatter elec_vconf_hat post_elec_ax if post_elec == 0 & biden_voter == 1, msymbol(D) mcolor(blue))
(rcap lb_ci95_elec_vconf_hat ub_ci95_elec_vconf_hat post_elec_ax if post_elec == 1 & biden_voter == 1, lcolor(gs6))
(rcap lb_ci95_elec_vconf_hat ub_ci95_elec_vconf_hat post_elec_ax if post_elec == 0 & biden_voter == 1, lcolor(gs6))
(scatter elec_vconf_hat post_elec_ax if post_elec == 1 & biden_voter == 0 & trump_voter == 0, mcolor(gray))  
(scatter elec_vconf_hat post_elec_ax if post_elec == 0 & biden_voter == 0 & trump_voter == 0, mcolor(gray)) 
(rcap lb_ci95_elec_vconf_hat ub_ci95_elec_vconf_hat post_elec_ax if post_elec == 1 & biden_voter == 0 & trump_voter == 0, lcolor(gs6))
(rcap lb_ci95_elec_vconf_hat ub_ci95_elec_vconf_hat post_elec_ax if post_elec == 0 & biden_voter == 0 & trump_voter == 0, lcolor(gs6)),
ytitle("Probability Very Confident" " ") title("All Respondents") xlabel(1 "Post-Election" 0 "Pre-Election") xtitle("") 
legend(  region(lcolor(white)) row(1) order(1 5 10) label(1 "Trump Voters") label(5 "Biden Voters") label(10 "Neither")) xsc(r(-0.25 1.25))
graphregion(color(white)) bgcolor(white)  name(vconf_nwide);


reg elec_vconf trumpv_post trump_voter bidenv_post biden_voter post_elec [aweight = weight] if state == "GA", robust;
predict elec_vconf_hat_GA, xb;
predict se_elec_vconf_hat_GA, stdp ;
gen lb_ci95_elec_vconf_hat_GA = elec_vconf_hat_GA - invttail(`e(N)', 0.975)*se_elec_vconf_hat_GA;
gen ub_ci95_elec_vconf_hat_GA = elec_vconf_hat_GA + invttail( `e(N)', 0.975)*se_elec_vconf_hat_GA;

*D-i-D estimator;
lincom bidenv_post - biden_voter - trumpv_post - trump_voter;

twoway (scatter elec_vconf_hat_GA post_elec_ax if post_elec == 1 & trump_voter == 1, msymbol(X) mcolor(red)) 
(scatter elec_vconf_hat_GA post_elec_ax if post_elec == 0 & trump_voter == 1, msymbol(X) mcolor(red)) 
(rcap lb_ci95_elec_vconf_hat_GA ub_ci95_elec_vconf_hat_GA post_elec_ax if post_elec == 1 & trump_voter == 1, lcolor(gs6))
(rcap lb_ci95_elec_vconf_hat_GA ub_ci95_elec_vconf_hat_GA post_elec_ax if post_elec == 0 & trump_voter == 1, lcolor(gs6))
(scatter elec_vconf_hat_GA post_elec_ax if post_elec == 1 & biden_voter == 1, msymbol(D) mcolor(blue))  
(scatter elec_vconf_hat_GA post_elec_ax if post_elec == 0 & biden_voter == 1, msymbol(D) mcolor(blue))
(rcap lb_ci95_elec_vconf_hat_GA ub_ci95_elec_vconf_hat_GA post_elec_ax if post_elec == 1 & biden_voter == 1, lcolor(gs6))
(rcap lb_ci95_elec_vconf_hat_GA ub_ci95_elec_vconf_hat_GA post_elec_ax if post_elec == 0 & biden_voter == 1, lcolor(gs6))
(scatter elec_vconf_hat_GA post_elec_ax if post_elec == 1 & biden_voter == 0 & trump_voter == 0, mcolor(gray))  
(scatter elec_vconf_hat_GA post_elec_ax if post_elec == 0 & biden_voter == 0 & trump_voter == 0, mcolor(gray)) 
(rcap lb_ci95_elec_vconf_hat_GA ub_ci95_elec_vconf_hat_GA post_elec_ax if post_elec == 1 & biden_voter == 0 & trump_voter == 0, lcolor(gs6))
(rcap lb_ci95_elec_vconf_hat_GA ub_ci95_elec_vconf_hat_GA post_elec_ax if post_elec == 0 & biden_voter == 0 & trump_voter == 0, lcolor(gs6)),
ytitle("") title("Georgia Respondents") xlabel(1 "Post-Election" 0 "Pre-Election") xtitle("") 
legend( region(lcolor(white)) row(1) order(1 5 10) label(1 "Trump Voters") label(5 "Biden Voters") label(10 "Neither")) xsc(r(-0.25 1.25))
graphregion(color(white)) bgcolor(white)  name(vconf_GA);




grc1leg vconf_nwide vconf_GA , legendfrom(vconf_nwide);


graph export "Elec_Vconf.pdf", replace;
